using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace MicroMsg.Manager
{
	public class ContactMgr
	{
		public enum UserType
		{
			UserTypeInvaild,
			UserTypeChatRoom,
			UserTypeGroupCard,
			UserTypeWeiXin,
			UserTypeTencent,
			UserTypeSina,
			UserTypeQQ,
			UserTypeBottle,
			UserTypePlugin,
			UserTypeNormal
		}
		public const string TAG = "ContactMgr";
		public const int MM_CONTACTFLAG_NULL = 0;
		public const int MM_CONTACT_WEIXIN = 0;
		public const int MM_CONTACT_QQMICROBLOG = 1;
		public const int MM_CONTACT_CHATROOM = 2;
		public const int MM_CONTACT_EMAIL = 3;
		public const int MM_CONTACT_QQ = 4;
		public const int MM_CONTACT_BOTTLE = 5;
		public const uint MM_CONTACTFLAG_CONTACT = 1u;
		public const uint MM_CONTACTFLAG_CHATCONTACT = 2u;
		public const uint MM_CONTACTFLAG_CHATROOMCONTACT = 4u;
		public const uint MM_CONTACTFLAG_BLACKLISTCONTACT = 8u;
		public const uint MM_CONTACTFLAG_DOMAINCONTACT = 16u;
		public const uint MM_CONTACTFLAG_HIDECONTACT = 32u;
		public const uint MM_CONTACTFLAG_FAVOURCONTACT = 64u;
		public const uint MM_CONTACTFLAG_3RDAPPCONTACT = 128u;
		public const uint MM_CONTACTFLAG_SNSBLACKLISTCONTACT = 256u;
		public const uint MM_CONTACTFLAG_ALL = 511u;
		public const int MM_CONTACTIMGFLAG_NOTMODIFY = 1;
		public const int MM_CONTACTIMGFLAG_MODIFY = 2;
		public const int MM_CONTACTIMGFLAG_HAS_HEADIMG = 3;
		public const int MM_CONTACTIMGFLAG_HAS_NO_HEADIMG = 4;
		public const int MM_CONTACTIMGFLAG_LOCAL_EXIST = 153;
		public const int MM_DOMAINEMAIL_NOTVERIFY = 1;
		public const int MM_DOMAINEMAIL_VERIFIED = 2;
		public const int MM_NOTIFY_CLOSE = 0;
		public const int MM_NOTIFY_OPEN = 1;
		public const int MM_DELFLAG_EXIST = 1;
		public const int MM_DELFLAG_DEL = 2;
		public const int MM_MICROBLOG_QQ = 1;
		public const int MMBRAND_SUBSCRIPTION_BLOCK_MESSAGE_NOTIFY = 1;
		public const int MMBRAND_SUBSCRIPTION_HIDE_FROM_PROFILE = 2;
		private EventWatcher mWatcherContact;
		public ObservableCollection<Contact> getList()
		{
			List<Contact> listWithOrder = StorageMgr.contact.getListWithOrder();
			if (listWithOrder != null)
			{
				return new ObservableCollection<Contact>(listWithOrder);
			}
			return null;
		}
		public static bool IsFmessageUser(string userName)
		{
			return !string.IsNullOrEmpty(userName) && userName == "fmessage";
		}
		public static ContactMgr.UserType getUserType(string userName)
		{
			if (userName == null || userName.get_Length() <= 0)
			{
				return ContactMgr.UserType.UserTypeInvaild;
			}
			if (userName.EndsWith("@chatroom"))
			{
				return ContactMgr.UserType.UserTypeChatRoom;
			}
			if (userName.EndsWith("@groupcard"))
			{
				return ContactMgr.UserType.UserTypeGroupCard;
			}
			if (userName.EndsWith("@micromsg.qq.com"))
			{
				return ContactMgr.UserType.UserTypeWeiXin;
			}
			if (userName.EndsWith("@t.qq.com"))
			{
				return ContactMgr.UserType.UserTypeTencent;
			}
			if (userName.EndsWith("@t.sina.com"))
			{
				return ContactMgr.UserType.UserTypeSina;
			}
			if (userName.EndsWith("@qqim"))
			{
				return ContactMgr.UserType.UserTypeQQ;
			}
			if (userName.Contains("@bottle"))
			{
				return ContactMgr.UserType.UserTypeBottle;
			}
			if (ToolFunction.systemPluginName.Contains(userName))
			{
				return ContactMgr.UserType.UserTypePlugin;
			}
			return ContactMgr.UserType.UserTypeNormal;
		}
		public static void setContact(Contact contact_0, bool isEnable)
		{
			if (isEnable)
			{
				contact_0.nFlags |= 1u;
				return;
			}
			contact_0.nFlags &= 4294967294u;
		}
		public static void operationSetContact(Contact contact_0, bool isEnable)
		{
			ContactMgr.setContact(contact_0, isEnable);
			StorageMgr.contact.update(contact_0);
			ConversationMgr.clearBackGround(contact_0.strUsrName, true);
			if (!OpLogMgr.OpModContact(contact_0, 511u))
			{
				Log.smethod_1("ContactMgr", "OpModContact failed when username=" + contact_0.strUsrName);
			}
		}
		public static bool isContact(Contact contact_0)
		{
			return (contact_0.nFlags & 1u) > 0u;
		}
		public static bool isFamous(int VerifyFlag)
		{
			return VerifyFlag != 0;
		}
		public static void setChatContact(Contact contact_0, bool isEnable)
		{
			if (isEnable)
			{
				contact_0.nFlags |= 2u;
				return;
			}
			contact_0.nFlags &= 4294967293u;
		}
		public static void operationSetChatContact(Contact contact_0, bool isEnable)
		{
			ContactMgr.setChatContact(contact_0, isEnable);
			StorageMgr.contact.update(contact_0);
			if (!OpLogMgr.OpModContact(contact_0, 511u))
			{
				Log.smethod_1("ContactMgr", "OpModContact failed when username=" + contact_0.strUsrName);
			}
		}
		public static bool isChatContact(Contact contact_0)
		{
			return (contact_0.nFlags & 2u) > 0u;
		}
		public static void setChatroomContact(Contact contact_0, bool isEnable)
		{
			if (contact_0 == null)
			{
				Log.smethod_1("ContactMgr", "setChatroomContact ct == null");
				return;
			}
			if (isEnable)
			{
				contact_0.nFlags |= 4u;
				return;
			}
			contact_0.nFlags &= 4294967291u;
		}
		public static void operationSetChatroomContact(Contact contact_0, bool isEnable)
		{
			if (contact_0 == null)
			{
				Log.smethod_1("ContactMgr", "operationSetChatroomContact ct == null");
				return;
			}
			ContactMgr.setChatroomContact(contact_0, isEnable);
			StorageMgr.contact.update(contact_0);
			if (!OpLogMgr.OpModContact(contact_0, 511u))
			{
				Log.smethod_1("ContactMgr", "OpModContact failed when username=" + contact_0.strUsrName);
			}
		}
		public static bool isChatroomContact(Contact contact_0)
		{
			return (contact_0.nFlags & 4u) > 0u;
		}
		public static void setBlackList(Contact contact_0, bool isEnable)
		{
			if (isEnable)
			{
				contact_0.nFlags |= 8u;
				return;
			}
			contact_0.nFlags &= 4294967287u;
		}
		public static void operationSetBlackList(Contact contact_0, bool isEnable)
		{
			ContactMgr.setBlackList(contact_0, isEnable);
			StorageMgr.contact.update(contact_0);
			if (!OpLogMgr.OpModContact(contact_0, 511u))
			{
				Log.smethod_1("ContactMgr", "OpModContact failed when username=" + contact_0.strUsrName);
			}
		}
		public static bool isBlackList(Contact contact_0)
		{
			return (contact_0.nFlags & 8u) > 0u;
		}
		public static void setDomainContact(Contact contact_0, bool isEnable)
		{
			if (isEnable)
			{
				contact_0.nFlags |= 16u;
				return;
			}
			contact_0.nFlags &= 4294967279u;
		}
		public static void operationSetDomainContact(Contact contact_0, bool isEnable)
		{
			ContactMgr.setDomainContact(contact_0, isEnable);
			StorageMgr.contact.update(contact_0);
			if (!OpLogMgr.OpModContact(contact_0, 511u))
			{
				Log.smethod_1("ContactMgr", "OpModContact failed when username=" + contact_0.strUsrName);
			}
		}
		public static bool isDomainContact(Contact contact_0)
		{
			return (contact_0.nFlags & 16u) > 0u;
		}
		public static void setHide(Contact contact_0, bool isEnable)
		{
			if (isEnable)
			{
				contact_0.nFlags |= 32u;
				return;
			}
			contact_0.nFlags &= 4294967263u;
		}
		public static void operationSetHide(Contact contact_0, bool isEnable)
		{
			ContactMgr.setHide(contact_0, isEnable);
			StorageMgr.contact.update(contact_0);
			if (!OpLogMgr.OpModContact(contact_0, 511u))
			{
				Log.smethod_1("ContactMgr", "OpModContact failed when username=" + contact_0.strUsrName);
			}
		}
		public static bool isHide(Contact contact_0)
		{
			return (contact_0.nFlags & 32u) > 0u;
		}
		public static void setFavourContact(Contact contact_0, bool isEnable)
		{
			if (isEnable)
			{
				contact_0.nFlags |= 64u;
				return;
			}
			contact_0.nFlags &= 4294967231u;
		}
		public static void operationSetFavourContact(Contact contact_0, bool isEnable)
		{
			ContactMgr.setFavourContact(contact_0, isEnable);
			StorageMgr.contact.update(contact_0);
			if (!OpLogMgr.OpModContact(contact_0, 511u))
			{
				Log.smethod_1("ContactMgr", "OpModContact failed when username=" + contact_0.strUsrName);
			}
		}
		public static bool isFavourContact(Contact contact_0)
		{
			return (contact_0.nFlags & 64u) > 0u;
		}
		public static bool isTWeiboShown(Contact contact_0)
		{
			return contact_0 != null && (contact_0.nWeiboFlag & 1u) == 1u;
		}
		public static bool operationModContact(Contact contact_0)
		{
			if (!OpLogMgr.OpModContact(contact_0, 511u))
			{
				Log.smethod_1("ContactMgr", "OpModContact failed when username=" + contact_0.strUsrName);
				return false;
			}
			return StorageMgr.contact.modify(contact_0);
		}
		public static bool operationModBrandSetting(Contact contact_0, uint BrandFlag)
		{
			if (!OpLogMgr.OpModBrandSetting(contact_0.strUsrName, BrandFlag))
			{
				Log.smethod_1("ContactMgr", "OpModContact failed when username=" + contact_0.strUsrName);
				return false;
			}
			contact_0.nBrandFlag = BrandFlag;
			return StorageMgr.contact.modify(contact_0);
		}
		public static void tryRefreshContact(Contact contact_0)
		{
			if (contact_0 == null)
			{
				return;
			}
			if (!ContactMgr.isContact(contact_0))
			{
				return;
			}
			if (contact_0.nUpdateDay == Util.getNowDays())
			{
				return;
			}
			NetSceneBatchGetContact arg_33_0 = ServiceCenter.sceneBatchGetContact;
			List<string> list = new List<string>();
			list.Add(contact_0.strUsrName);
			arg_33_0.doScene(list);
		}
		public static bool IsShowInMyProfile(Contact contact_0)
		{
			return contact_0 != null && ContactMgr.IsShowInMyProfile(contact_0.nBrandFlag);
		}
		public static bool IsShowInMyProfile(uint nFlag)
		{
			return (~nFlag & 2u) > 0u;
		}
		public static void SetShowInMyProfile(Contact contact_0, bool bShowInProfile)
		{
			if (contact_0 == null)
			{
				return;
			}
			if (bShowInProfile)
			{
				contact_0.nBrandFlag &= 4294967293u;
			}
			else
			{
				contact_0.nBrandFlag |= 2u;
			}
			ContactMgr.operationModBrandSetting(contact_0, contact_0.nBrandFlag);
			Account curAccount = AccountMgr.getCurAccount();
			Contact contact = StorageMgr.contact.method_0(AccountMgr.strUsrName);
			if (contact != null && curAccount != null)
			{
				contact.updateBrand(new BrandUser
				{
					strUsrName = contact_0.strUsrName,
					nHidden = bShowInProfile ? 0u : 1u,
					strNickName = contact_0.strNickName,
					strBrandIconURL = contact_0.strBrandIconURL
				});
				StorageMgr.contact.update(contact);
				if (curAccount != null)
				{
					curAccount.MyBrandList = contact.strMyBrandList;
				}
				AccountMgr.updateAccount();
			}
		}
		public static bool IsReceiveMsg(Contact contact_0)
		{
			return contact_0 != null && ContactMgr.IsReceiveMsg(contact_0.nBrandFlag);
		}
		public static bool IsReceiveMsg(uint nFlag)
		{
			return (~nFlag & 1u) > 0u;
		}
		public static void SetReceiveMsg(Contact contact_0, bool bReceive)
		{
			if (contact_0 == null)
			{
				return;
			}
			if (bReceive)
			{
				contact_0.nBrandFlag &= 4294967294u;
			}
			else
			{
				contact_0.nBrandFlag |= 1u;
			}
			ContactMgr.operationModBrandSetting(contact_0, contact_0.nBrandFlag);
		}
		public static ListBrandLinks GetBrandCustomLinks(string strJson)
		{
			return (ListBrandLinks)Util.GetObjectFromJson(strJson, typeof(ListBrandLinks));
		}
		public static BrandCustomSetting GetBrandCustomSetting(string strJson)
		{
			return (BrandCustomSetting)Util.GetObjectFromJson(strJson, typeof(BrandCustomSetting));
		}
		public static int getCheckSum(Contact contact_0)
		{
			return Util.CheckSum(string.Concat(new object[]
			{
				contact_0.strUsrName,
				contact_0.strNickName,
				contact_0.strSignature,
				contact_0.strCity,
				contact_0.strProvince,
				contact_0.nSex,
				contact_0.nFlags,
				contact_0.nSnsFlag,
				contact_0.strMyBrandList,
				contact_0.strSnsBGImgID,
				contact_0.strAlias,
				contact_0.strMobile,
				contact_0.nWeiboFlag,
				contact_0.strWeibo,
				contact_0.strWeiboNickname
			}));
		}
		public static Contact GetContactFromAccount(bool bIsSyncToStorage = false)
		{
			Account curAccount = AccountMgr.getCurAccount();
			Contact contact = StorageMgr.contact.method_0(AccountMgr.strUsrName);
			int num = 0;
			if (contact == null)
			{
				contact = new Contact();
				bIsSyncToStorage = true;
			}
			else
			{
				num = ContactMgr.getCheckSum(contact);
			}
			contact.strUsrName = curAccount.strUsrName;
			contact.strNickName = curAccount.strNickName;
			contact.strSignature = curAccount.strSignature;
			contact.strCity = curAccount.strCity;
			contact.strProvince = curAccount.strProvince;
			contact.nSex = curAccount.nSex;
			contact.nFlags = 7u;
			contact.nSnsFlag = curAccount.nSnsFlag;
			contact.strMyBrandList = curAccount.MyBrandList;
			contact.strSnsBGImgID = curAccount.strSnsBGImgID;
			contact.strAlias = curAccount.strAlias;
			contact.strMobile = curAccount.strBindMobile;
			contact.nWeiboFlag = curAccount.nWeiboFlag;
			contact.strWeibo = curAccount.strWeibo;
			contact.strWeiboNickname = curAccount.strWeiboNickname;
			if (num == ContactMgr.getCheckSum(contact))
			{
				return contact;
			}
			if (bIsSyncToStorage)
			{
				StorageMgr.contact.update(contact);
			}
			return contact;
		}
	}
}
